package com.sjgtw.cloud.file.service.impl;

import com.sjgtw.cloud.file.config.MinioConfig;
import com.sjgtw.cloud.file.service.ISysFileService;
import com.sjgtw.cloud.file.utils.FileUploadUtils;
import io.minio.*;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * <p>Title: 文件管理--服务层--接口实现</p>
 * <p>Description: 使用 Minio文件存储服务器</p>
 * @author Admin
 * @date 2020-7-7
 */
@Primary
@Service
public class MinioSysFileServiceImpl implements ISysFileService
{
	@Autowired
	private MinioConfig minioConfig;

	@Autowired
	private MinioClient client;

	/**
	 * 本地文件上传接口
	 * 
	 * @param file 上传的文件
	 * @return 访问地址
	 * @throws Exception
	 */
	@Override
	public String uploadFile(MultipartFile file) throws Exception {
			String fileName = FileUploadUtils.extractFilename(file);
		System.out.println(file.getContentType());
		PutObjectArgs args = PutObjectArgs.builder().bucket(minioConfig.getBucketName()).object(fileName)
				.stream(file.getInputStream(), file.getSize(), -1).contentType(file.getContentType()).build();
		client.putObject(args);
		return minioConfig.getReadUrl() + "/" + minioConfig.getBucketName() + "/" + fileName;
	}

	@Override
	public InputStream download(String objectName) {
		String bucketName = minioConfig.getBucketName();
		boolean flag = bucketExists(bucketName);
		if (flag) {
			StatObjectResponse statObject = statObject(bucketName, objectName);
			if (statObject != null && statObject.size() > 0) {
				InputStream stream =
						null;
				try {
					stream = client.getObject(
							GetObjectArgs.builder()
									.bucket(bucketName)
									.object(objectName)
									.build());
				} catch (ErrorResponseException e) {
					e.printStackTrace();
				} catch (InsufficientDataException e) {
					e.printStackTrace();
				} catch (InternalException e) {
					e.printStackTrace();
				} catch (InvalidKeyException e) {
					e.printStackTrace();
				} catch (InvalidResponseException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} catch (NoSuchAlgorithmException e) {
					e.printStackTrace();
				} catch (ServerException e) {
					e.printStackTrace();
				} catch (XmlParserException e) {
					e.printStackTrace();
				}
				return stream;
			}
		}
		return null;
	}

	/**
	 * 获取对象的元数据
	 *
	 * @param bucketName 存储桶名称
	 * @param objectName 存储桶里的对象名称
	 * @return
	 */
	public StatObjectResponse statObject(String bucketName, String objectName) {
		boolean flag = bucketExists(bucketName);
		if (flag) {
			StatObjectResponse stat =
					null;
			try {
				stat = client.statObject(
						StatObjectArgs.builder().bucket(bucketName).object(objectName).build());
			} catch (ErrorResponseException e) {
				e.printStackTrace();
			} catch (InsufficientDataException e) {
				e.printStackTrace();
			} catch (InternalException e) {
				e.printStackTrace();
			} catch (InvalidKeyException e) {
				e.printStackTrace();
			} catch (InvalidResponseException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (NoSuchAlgorithmException e) {
				e.printStackTrace();
			} catch (ServerException e) {
				e.printStackTrace();
			} catch (XmlParserException e) {
				e.printStackTrace();
			}
			return stat;
		}
		return null;
	}

	/**
	 * 检查存储桶是否存在
	 *
	 * @param bucketName 存储桶名称
	 * @return
	 */
	public boolean bucketExists(String bucketName) {
		boolean found = false;
		try {
			found = client.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
		} catch (ErrorResponseException e) {
			e.printStackTrace();
		} catch (InsufficientDataException e) {
			e.printStackTrace();
		} catch (InternalException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (InvalidResponseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (ServerException e) {
			e.printStackTrace();
		} catch (XmlParserException e) {
			e.printStackTrace();
		}
		if (found) {
			System.out.println(bucketName + " exists");
		} else {
			System.out.println(bucketName + " does not exist");
		}
		return found;
	}
}
